import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd/message';
import {ParkingChargeService} from '../../service/parking-charge.service';
import {ParkingChargeQueryForm} from '../../types/req/ParkingChargeQueryForm';
import {SharedModule} from '../shared.module';
import {ParkingCharge} from '../../types/resp/ParkingCharge';
import {Page} from '../../types/page';
import {NzModalService} from 'ng-zorro-antd/modal';
import {MODAL_WIDTH} from '../../const';
import {ParkingAreaSelectorComponent} from '../parking-area/parking-area-selector.component';
import {RoomSelectorComponent} from '../room/room-selector.component';

@Component({
  selector: 'parking-charge-selector',
  imports: [
    ParkingAreaSelectorComponent,
    RoomSelectorComponent,
    SharedModule
  ],
  templateUrl: './parking-charge-selector.component.html',
  standalone: true
})
export class ParkingChargeSelectorComponent implements OnInit {
  searchForm!: FormGroup;
  searchLoading: boolean = false
  listOfData: ParkingCharge[] = [];
  total: number = 0
  isVisible: boolean = false
  title: string = '车位收费信息选择器(双击行选中)'
  modalWidth: string = MODAL_WIDTH

  @ViewChild(ParkingAreaSelectorComponent, { static: false }) parkingAreaSelectorComponent!: ParkingAreaSelectorComponent;
  @ViewChild(RoomSelectorComponent, { static: false }) roomSelectorComponent!: RoomSelectorComponent;

  @Output() parkingChargeSelectedEvent = new EventEmitter<{}>();

  constructor(
    private fb: FormBuilder,
    private message: NzMessageService,
    private parkingChargeService: ParkingChargeService,
    private modal: NzModalService
  ) {}

  ngOnInit(): void {
    this.searchForm = this.fb.group({
      parkingAreaId: [0],
      parkingAreaNumber: [''],
      residentRoomId: [0],
      residentRoomNumber: [''],
      licensePlate: [''],
    })
    this.onSearch()
  }

  onReset() {
    this.searchForm.reset()
  }

  onSearch() {
    try {
      this.searchLoading = true
      this.parkingChargeService.find(new ParkingChargeQueryForm(this.searchForm.value)).subscribe({
        next: (resp) => {
          if (!resp || resp.code !== 1) {
            this.message.error(resp && resp.msg ? resp.msg : '操作异常')
            return
          }
          const page: Page<ParkingCharge> | undefined = resp.data
          if (page && page.list.length > 0) {
            this.listOfData = page.list
            this.total = page.total
          }
        },
        complete: () => {
          this.searchLoading = false
        }
      })
    } catch (e) {
      console.log('获取数据异常', e)
    }
  }

  onRowDblClick(row: ParkingCharge, event: MouseEvent) {
    this.isVisible = false
    this.parkingChargeSelectedEvent.emit({
    });
  }

  onBack() {
    this.isVisible = false
  }

  display() {
    this.isVisible = true
    this.searchForm.reset();
  }

  findParkingArea() {
    this.parkingAreaSelectorComponent.display()
  }

  handleParkingAreaSelectedEvent(selectedParkingArea: { parkingAreaId?: number; parkingAreaNumber?: string; }) {
    if (selectedParkingArea && 'parkingAreaId' in selectedParkingArea) {
      this.searchForm.patchValue(selectedParkingArea);
    }
  }
  findRoom() {
    this.roomSelectorComponent.display()
  }

  handleRoomSelectedEvent(selectedRoom: { residentRoomId?: number; residentRoomNumber?: string; }) {
    if (selectedRoom && 'residentRoomId' in selectedRoom) {
      this.searchForm.patchValue(selectedRoom);
    }
  }
}
